package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.homework.bean.ClassWorkContent;
import com.ciwong.epaper.modules.homework.bean.ClassWorkRecord;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;

import java.net.URLEncoder;

/**
 * @description:课堂作业详情页面
 * @author: wyk
 * @time: 2021/12/28 9:52
 */
public class ClassWorkGradesActivity extends BaseHtmlActicity {
    //课堂作业详情
    private ClassWorkRecord classWorkRecord;
    //课堂作业内容
    private ClassWorkContent classWorkContent;

    private String accessToken;

    @Override
    protected void preCreate() {
        super.preCreate();
        Intent intent = getIntent();
        classWorkContent = (ClassWorkContent) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_WORK_CONTENT);
        classWorkRecord = (ClassWorkRecord) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_CLASS_WORK_RECORD);
    }

    @Override
    protected void init() {
        super.init();
        //setValideSource(false);
        setTitleText(R.string.work_details);
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            try {
                String clientId = verifyInfo.getClientId();
                accessToken = verifyInfo.getAccessToken();
                String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
                String brandId = EApplication.BRAND_ID + "";
                String className = toUtf8(classWorkRecord.getClassName());
                String startUrl = EpaperConstant.URL_H5_CLASS_WORK_GRADES + "clientId="+clientId+"&accessToken="+accessToken
                        +"&brandId="+brandId + "&contentId="+classWorkContent.getContentId()+"&workId=" + classWorkRecord.getWorkId()
                        +"&versionId="+classWorkContent.getVersionId() + "&resType=" + classWorkContent.getResourceType() +"&from="+ 1 +
                        "&packageId=" + classWorkContent.getPackageId() + "&cId="+ classWorkContent.getcId() + "&className=" +className;
                setStartURL(startUrl);
                loadUrl(startUrl);
            }catch (Exception e){
                e.printStackTrace();
            }

        } else {
            showToastError(getString(R.string.get_user_info_error));
            finish();
        }
    }

    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str,"UTF-8");
            result = result.replace("+", "%20");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }



    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        CWLog.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("JumpClassWorkGrades")) {//
            jumpToInnerResouce(submitEvent.getJson());
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);
    }
}
